package cn.cdyx.common.utils;

import cn.cdyx.common.cache.TbParamTableCache;
import cn.cdyx.common.pojo.TBUSERPARAM;
import cn.cdyx.common.pojo.TBYXXTCSI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Objects;

/**
 * @Author Watermelon
 * @Date 2023-01-14 14:57
 * 参数表工具。建议：非必须情况 还是使用nacos配置中心的动态参数加载，更加灵活快捷
 */
@Slf4j
@Component
public class YXParamUtil {
    @Autowired
    private TbParamTableCache tbParamTableCache;

    private static YXParamUtil yxParamUtils;

    @PostConstruct
    public void init(){
        yxParamUtils = this;
        yxParamUtils.tbParamTableCache = this.tbParamTableCache;
    }

    /**
     * 用户参数照HIS命名
     * @return
     */
    public static String getUserParam(String paramName,String defValue){
        TBUSERPARAM tbuserparam = null;
        for (TBUSERPARAM tbuserparam1 : yxParamUtils.tbParamTableCache.tbuserparams) {
            if (Objects.equals(tbuserparam1.getCnbmc(),paramName)) {
                tbuserparam = tbuserparam1;
                break;
            }
        }
        if(tbuserparam == null){
            return defValue;
        }
        return tbuserparam.getCvalue();
    }

    /**
     * 系统参数照HIS命名
     * @return
     */
    public static String getXtchcsExI(String paramName,String defValue){
        TBYXXTCSI tbyxxtcsi = null;
        for (TBYXXTCSI tbyxxtcsi1 : yxParamUtils.tbParamTableCache.tbyxxtcsis) {
            if (Objects.equals(tbyxxtcsi1.getCcsmc(),paramName)) {
                tbyxxtcsi = tbyxxtcsi1;
                break;
            }
        }
        if(tbyxxtcsi == null){
            return defValue;
        }
        return tbyxxtcsi.getCvalue();
    }

    /**
     * 用户参数照HIS命名 返回对象
     * @return
     */
    public static @Nullable TBUSERPARAM getUserParamObj(String paramName){
        TBUSERPARAM tbuserparam = null;
        for (TBUSERPARAM tbuserparam1 : yxParamUtils.tbParamTableCache.tbuserparams) {
            if (Objects.equals(tbuserparam1.getCnbmc(),paramName)) {
                tbuserparam = tbuserparam1;
                break;
            }
        }
        return tbuserparam;
    }

    /**
     * 系统参数照HIS命名 返回对象
     * @return
     */
    public static @Nullable TBYXXTCSI getXtchcsExIObj(String paramName){
        TBYXXTCSI tbyxxtcsi = null;
        for (TBYXXTCSI tbyxxtcsi1 : yxParamUtils.tbParamTableCache.tbyxxtcsis) {
            if (Objects.equals(tbyxxtcsi1.getCcsmc(),paramName)) {
                tbyxxtcsi = tbyxxtcsi1;
                break;
            }
        }
        return tbyxxtcsi;
    }
}
